<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="经营者名称" prop="certificateNo">
        <el-input
          v-model="queryParams.businessName"
          placeholder="经营者名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:SettleAccountInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:SettleAccountInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:SettleAccountInfo:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="licenseInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="营业执照名称" align="center" prop="businessLicenseName" />
      <el-table-column label="开始日期" align="center" prop="businessLicenseStartDate" />
      <el-table-column label="截止日期" align="center" prop="businessLicenseEndDate" />
      <el-table-column label="经营者名称" align="center" prop="businessName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:SettleAccountInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:SettleAccountInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改结算信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="社会统一信用码" prop="businessLicenseNo">
          <el-input v-model="form.businessLicenseNo" placeholder="请输入社会统一信用码" />
        </el-form-item>
        <el-form-item label="营业执照名称" prop="businessLicenseName">
          <el-input v-model="form.businessLicenseName" placeholder="请输入营业执照名称" />
        </el-form-item>
        <el-form-item label="开始日期" prop="certificateStartDate">
          <el-date-picker clearable size="small"
                          v-model="form.businessLicenseStartDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="截止日期" prop="certificateEndDate">
          <el-date-picker clearable size="small"
                          v-model="form.businessLicenseEndDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="截止日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="经营者名称" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入经营者名称" />
        </el-form-item>
        <el-form-item label="营业执照照片">
          <el-upload
            :file-list="businessCreditLicensePicList"
            :on-change="(file,fileList)=>{return uploadChange(file,fileList)}"
            :on-remove="(file,fileList)=>{return handleRemove(file,fileList)}"
            action="#"
            ref="businessCreditLicensePic"
            list-type="picture-card"
            :on-preview="imgPreview"
            :data="form"
            :http-request="(file)=>{return businessCreditLicensePicHttpRequest(file)}"
            :auto-upload="false">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="imgVisible">
            <img width="100%" :src="imgPath" alt="">
          </el-dialog>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listBziLicenseInfo, getBziLicenseInfo, addBziLicenseInfo, updateBziLicenseInfo, delBziLicenseInfo } from "@/api/posp/BizLicenseInfo";

  export default {
    name: "BziLicenseInfo",
    dicts: ['idCard_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 法人信息表格数据
        licenseInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          businessName: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        imgVisible:false,
        imgPath:null,
        formData : new FormData(),
        businessCreditLicensePicList : []
      };
    },
    created() {
      this.getList();
      this.form.merchantId = this.$route.query.merchantId;
    },
    methods: {
      beforeUpload(file,fileList){
        let flag = true;
        let isLt2M = file.size / 1024  < 500;
        if (!isLt2M) {
          this.$modal.msgError("上传图片或视频大小不能超过500Kb'!")
          flag = false;
        }
        // 验证是否是图片
        if (file.type.indexOf("image/") == -1) {
          this.$modal.msgError("文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。")
          flag = false;
        }
        if(flag){
          if (fileList.length > 1) {
            fileList.splice(0, 1)
          }
        }else{
          fileList.splice(fileList.length-1, 1);
        }
      },
      imgPreview(file) {
        this.imgPath = file.url;
        this.imgVisible = true;
      },
      businessCreditLicensePicHttpRequest(param){
        this.formData.append('businessCreditLicensePicFile', param.file);
      },
      handleRemove(file, fileList) {
        if(fileList.length == 0){
          this.form.certificatesBackPicLocal = ''
        }
      },
      uploadChange(file, fileList) {
        this.beforeUpload(file.raw,fileList);
      },
      /** 查询结算信息列表 */
      getList() {
        this.loading = true;
        listBziLicenseInfo(this.queryParams).then(response => {
          this.licenseInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: '',
          businessLicenseNo: '',
          businessLicenseName: '',
          businessLicenseStartDate: '',
          businessLicenseEndDate: '',
          businessName: '',
          businessCreditLicensePic: '',
          businessCreditLicensePicLocal:'',
          merchantId: ''
        };
        this.resetForm("form");
        this.formData = new FormData();
        this.businessCreditLicensePicList = [];
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加营业执照信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getBziLicenseInfo(id).then(response => {
          this.form = response.data;
          if(this.form.businessCreditLicensePicLocal){
            let obj1 = {
              url:process.env.VUE_APP_BASE_API + this.form.businessCreditLicensePicLocal
            };
            this.businessCreditLicensePicList.push(obj1);
          }
          this.open = true;
          this.title = "修改营业执照信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.$refs.businessCreditLicensePic.submit();
            this.formData.append('id',this.form.id);
            this.formData.append('businessLicenseNo',this.form.businessLicenseNo);
            this.formData.append('businessLicenseName',this.form.businessLicenseName);
            this.formData.append('businessLicenseStartDate',this.form.businessLicenseStartDate);
            this.formData.append('businessLicenseEndDate',this.form.businessLicenseEndDate);
            this.formData.append('businessName',this.form.businessName);
            this.formData.append('merchantId',this.$route.query.merchantId);
            if (this.form.id != null && this.form.id != '') {
              updateBziLicenseInfo(this.formData).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
            } else {
              addBziLicenseInfo(this.formData).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除数据项？').then(function() {
          return delBziLicenseInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    }
  };
</script>
